import 'package:dio/dio.dart';
import 'package:verse_systems/commom/routers/navigator_utils.dart';
import 'package:verse_systems/net/http_result_data.dart';
import 'package:verse_systems/net/src/sparrow_dio_config.dart';
import 'package:verse_systems/utils/log_utils.dart';

InterceptorsWrapper tokenInterceptor = InterceptorsWrapper(
  //请求前
  onRequest: (options, handler) {
    LogD('请求URL:${options.uri.toString()}');
    LogD('请求值:${options.queryParameters.toString()}');
    LogD('请求body值:${options.data.toString()}');

    bool? needToken = options.extra['needToken'];
    if (needToken == false) {
      return handler.next(options);
    } else {
      var token = options.extra['token'] ?? SparrowDioConfig.token;
      options.headers["token"] = token;
      LogD("请求头:${options.headers.toString()}");
      return handler.next(options);
    }
  },
  //响应前
  onResponse: (response, handler) {
    try {
      final responseJson = response.data;
      LogD("Interceptor网络请求返回值:${response.data.toString()}");
      ResultData commResult = ResultData.fromJson(responseJson);
      if (commResult.status == 800004) {
        LogE('tokenInterceptor错误日志--登录过期,请重新登录');
          SparrowDioConfig.output('登录失效');
         NavigatorUtils.hook401();
        return;
      }
    } catch (e) {
      LogE('tokenInterceptor错误日志--${e.toString()}');
    }
    return handler.next(response);
  },
);
